package com.md.album.ui;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.md.album.R;
import com.md.album.SelectCoverModel;
import com.md.album.adapter.SelectCoverAdapter;
import com.md.basedpc.base.BaseActivity;
import com.md.livingwidget.navigation.PHNavigationBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SelectCoverActivity extends BaseActivity {

    private ImageView ivBigCon;
    private RecyclerView recyclerView;
    private List<String> iconResIds = new ArrayList<>();
    private SelectCoverAdapter adapter;
    private PHNavigationBar navBar;
    public static String resUrl = "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/book.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_avatar);
        EventBus.getDefault().register(this);
        initData();
        initView();

    }

    private void initData() {
        iconResIds.addAll(Arrays.asList("https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/bear.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/book.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/cat.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/fall.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/key.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/sheep.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/star.png",
                "https://goodland-res-1304950940.cos.ap-shanghai.myqcloud.com/tree.png"));
    }

    private void initView() {
        navBar = findViewById(R.id.navBar);
        navBar.getRight1Tv().setVisibility(View.VISIBLE);
        navBar.getRight1Tv().setTextColor(getResources().getColor(R.color.album_595959));
        navBar.getRight1Tv().setText(getResources().getString(R.string.album_save));
        navBar.getRight1Tv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new SelectCoverModel(resUrl, true));
                finish();
            }
        });
        navBar.getCenterTvTitle().setText(getResources().getString(R.string.album_select_icon));
        navBar.getLeft1Imgback().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivBigCon = findViewById(R.id.ivBigIcon);
        Glide.with(this)
                .load(R.drawable.book)
                .centerCrop()
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(ivBigCon);
        recyclerView = findViewById(R.id.rec);
        LinearLayoutManager mLayoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(mLayoutManager);
        adapter = new SelectCoverAdapter(this, iconResIds);
        recyclerView.setAdapter(adapter);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshIcon(SelectCoverModel model) {
        this.resUrl = model.resUrl;
        Glide.with(this)
                .load(resUrl)
                .centerCrop()
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(ivBigCon);
    }


}
